package mylab.starters.uuid.impl.snowflake;

import mylab.starters.uuid.AbstractUuidTemplate;
import mylab.starters.uuid.Uuid;
import mylab.starters.uuid.UuidProvider;
import mylab.starters.uuid.config.UuidProperties;
import mylab.starters.uuid.model.Prefix;
import mylab.starters.uuid.model.impl.DefaultPrefix;
import mylab.starters.uuid.model.impl.SnowFlakeId;
import mylab.utils.spring.spi.SpiProvider;

public class SnowflakeUuidTemplate extends AbstractUuidTemplate<UuidProperties.SnowflakeUuidProperties> {

    /**
     * 数据中心ID，值的范围在[0,31]之间，一般可以设置机房的IDC[必选]
     */
    private long dataCenterId;
    /**
     * 工作机器ID，值的范围在[0,31]之间，一般可以设置机器编号[必选]
     */
    private long workerId;
    /**
     * 前缀，可选，默认为空
     */
    private Prefix prefix = new DefaultPrefix();

    public SnowflakeUuidTemplate() {
    }

    public SnowflakeUuidTemplate(UuidProperties.SnowflakeUuidProperties properties) {
        super(properties);
        this.dataCenterId = properties.getDataCenterId();
        this.workerId = properties.getWorkerId();
        this.prefix = new DefaultPrefix(properties.getPrefix());
    }

    public static SnowflakeUuidTemplate create() {
        return new SnowflakeUuidTemplate();
    }

    public SnowflakeUuidTemplate dataCenterId(long dataCenterId) {
        this.dataCenterId = dataCenterId;
        return this;
    }

    public SnowflakeUuidTemplate workerId(long workerId) {
        this.workerId = workerId;
        return this;
    }

    public SnowflakeUuidTemplate prefix(Prefix prefix) {
        this.prefix = prefix;
        return this;
    }

    @Override
    public Uuid getInstance() {
        return new SnowFlakeId(this.dataCenterId, this.workerId, this.prefix);
    }

    @Override
    public SpiProvider getProvider() {
        return UuidProvider.Snowflake;
    }
}
